var app = angular.module('fiveApp');
app.controller('uploadCtrl', ['$rootScope', '$scope', 'ajaxService', '$stateParams', function ($rootScope, $scope, ajax, $stateParams) {

    $scope.data = {
        mask: 1,
        zheng: '',type:['注销','注册','变更'],typeName:'',
        fan: '', order_sn: ''
    };

    $scope.$on('$stateChangeSuccess', function () {
        $scope.data.order_sn = $stateParams.query;
        if ($scope.data.order_sn == '') return $scope.location('/')
        ajax('indexOrderInfo',{order_sn:$scope.data.order_sn},function(res){
            if(!res.errCode && res.data){
                $scope.data.typeName = $scope.data.type[res.data.type-2]
            }else{
                return $scope.location('/')
            }
        })
    })
    $scope.chooseImage = function (pos) {
        if ($scope.data.order_sn == '') return $scope.location('/')
        wx.chooseImage({
            count: 1,
            success: function (rel) {
                $.iBox.loading('上传中...')
                var localId = rel.localIds[0];
                wx.getLocalImgData({
                    localId: localId,
                    success: function (res) {
                        if (res.localData.indexOf('data:image') == -1) {
                            //res.localData = 'data:image/png;base64,'+res.localData;
                        } else {
                            let local = res.localData.split(',');
                            res.localData = local[1];
                        }
                        ajax('indexUpload', {
                            code: res.localData,
                        }, function (res) {
                            if (!res.errCode) {
                                $.iBox.success('上传成功');
                                if (pos == 1) $scope.data.zheng = res.data
                                else $scope.data.fan = res.data
                            } else {
                                $.iBox.error('上传失败');
                            }
                        })
                    }
                })
            }
        })
    }
    $scope.submit = function () {
        if ($scope.data.order_sn == '') return $scope.location('/')
        if ($scope.data.zheng == '') return $.iBox.error('请上传身份证正面照片');
        if ($scope.data.fan == '') return $.iBox.error('请上传身份证反面照片');

        ajax('indexOrderIdCard', {order_sn: $scope.data.order_sn, zheng: $scope.data.zheng, fan: $scope.data.fan}, function (res) {
            if (!res.errCode) {
                $.iBox.success('上传成功');
                setTimeout(() => {
                    $scope.location('/form/index/'+res.data.type)
                }, 1000)
            }
        })
    }
}
])